package com.wl.shop.utils;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.wl.shop.dao.FieldArticleDao;
import com.wl.shop.dao.FieldCategoryDao;
import com.wl.shop.entity.FieldArticle;
import com.wl.shop.entity.FieldCategory;

public class ArticleUtils {
	
	private static FieldCategoryDao fieldCategoryDao = SpringContextHolder.getBean(FieldCategoryDao.class);
	
	private static FieldArticleDao fieldArticleDao = SpringContextHolder.getBean(FieldArticleDao.class);
	
	public static final String CACHE_FIELD_MAP = "fieldMap";
	
	public static List<FieldCategory> getFieldList(String categoryId){
		@SuppressWarnings("unchecked")
		Map<String, List<FieldCategory>> fieldMap = (Map<String, List<FieldCategory>>)CacheUtils.get(CACHE_FIELD_MAP);
		if (fieldMap==null){
			fieldMap = Maps.newHashMap();
			for (FieldCategory fc : fieldCategoryDao.selectList(new EntityWrapper<FieldCategory>())) {
				List<FieldCategory> fcList = fieldMap.get(fc.getCategoryId());
				if(fcList != null){
					fcList.add(fc);
				}else{
					fieldMap.put(fc.getCategoryId(), Lists.newArrayList(fc));
				}
			}
			CacheUtils.put(CACHE_FIELD_MAP, fieldMap);
		}
		List<FieldCategory> list = fieldMap.get(categoryId);
		if(list == null){
			list =  Lists.newArrayList();
		}
		return list;
	}
	
	public static String getFieldArticle(Long articleId,String fieldEnName){
		FieldArticle fa = new FieldArticle();
		fa.setArticleId(articleId);
		fa.setFieldEnName(fieldEnName);
		FieldArticle f  = fieldArticleDao.selectOne(fa);
		if(f != null && StringUtils.isNotBlank(f.getContent())){
			return f.getContent();
		}else{
			return "";
		}
	}
}
